<?php
// function __autoload($class_name) {
//         include 'class.' . strtolower($class_name) . '.php';
// }

class UsulanKppnDao {

	public function getAll(){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan_kppn ";


        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
	}

    

	public function getAllByUnit($unit){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan_kppn WHERE unit='$unit'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
	}

	public function getById($id){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan_kppn WHERE id='$id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return $result->fetch();
        }

        return false;
	}

    public function createUsulan(UsulanKppn $usulan){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $date_created=date('Y-m-d H:i:s');
        $date=date('Y-m-d');

        $query="INSERT INTO t_usulan_kppn(nomorSurat,unit,tglSurat,tglProses,date_created) ";
        $query.=" VALUES('$usulan->nomorSurat','$usulan->unit','$usulan->tglSurat','$date','$date_created')";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return $con->lastInsertId();;
        }

        return false;
    }

	public function updateUsulan(UsulanKppn $usulan){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $date_updated=date('Y-m-d H:i:s');

        $query="UPDATE t_usulan_kppn SET nomorSurat='$usulan->nomorSurat',tglSurat='$usulan->tglSurat',date_updated='$date_updated' WHERE id='$id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
	}

    public function deleteUsulan($id){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="DELETE FROM t_usulan_kppn WHERE id='$id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function kirimUsulan($id){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="UPDATE t_usulan_kppn set kirim=1 WHERE id='$id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

}

class UsulanKppn {
	var $id;
	var $unit;
	var $nomorSurat;
	var $tglSurat;

}

?>